package spms.front.controller;

import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import spms.data.SpmsDataRemoteApi;
import spms.data.entity.Icon;
import spms.front.util.Ajax;
import spms.util.Page;

import javax.annotation.Resource;

@Api(tags = "Icon")
@Controller
public class IconController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    SpmsDataRemoteApi spmsDataRemoteApi;

    @GetMapping("/iconList")
    public Object getIconList(@RequestParam(defaultValue = "0") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              Model model) {

        logger.info("get请求/iconList");
        iconList(pageNum, pageSize, model);
        return "iconList";
    }

    @PostMapping("/iconList")
    public Object iconList(@RequestParam(defaultValue = "0") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           Model model) {

        logger.info("post请求/iconList");

        Page<Icon> page = spmsDataRemoteApi.findIconPage(pageNum, pageSize);

        model.addAttribute("page", page);
        return "iconList::iconList";
    }

    @ResponseBody
    @PostMapping("/addIcon")
    public Ajax addIcon(Model model, @RequestBody Icon icon) {
        try {
            spmsDataRemoteApi.addIcon(icon);
        } catch (Exception e) {
            logger.error("新增icon失败", e);
        }
        return new Ajax();
    }
}
